package com.service.sso.sdk.service;

import com.alibaba.fastjson.JSONObject;
import com.service.sso.sdk.config.SSOConfig;
import com.service.sso.sdk.dto.UserDto;
import com.tool.base.library.http.HttpModel;
import com.tool.base.library.http.HttpResponse;
import com.tool.base.library.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SsoService {

    @Autowired
    private HttpUtil httpUtil;
    @Autowired
    private SSOConfig ssoConfig;

    public UserDto getUser(String token) {
        HttpModel httpModel = new HttpModel();
        try {
            httpModel.getQueryParam().put("token", token);
            HttpResponse response = httpUtil.get(ssoConfig.getVerifyToken(), httpModel);
            String body = response.getBody();
            log.warn("{}", body);
            JSONObject jsonObject = JSONObject.parseObject(body);
            return jsonObject.getObject("data", UserDto.class);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("{}", e.getMessage());
        }
        return null;
    }

    public void logout(String token) {
        HttpModel httpModel = new HttpModel();
        try {
            httpModel.getQueryParam().put("token", token);
            HttpResponse response = httpUtil.delete(ssoConfig.getLogoutUrl(), httpModel);
            String body = response.getBody();
            log.warn("{}", body);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
